#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using pii = pair < int , int >;
using vec = vector < int >;
using veg = vector < pii >;
using ll = long long;

int read() {
    int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
    while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}

ll s, n;

void solve() {
	cin >> n;
	if(n == 2) 
		rep(i, 1, n) { rep(j, 1, n) cout << -1 << " " ;  cout << endl; }
	else {
		rep(i, 1, n) { rep(j, 1, n) cout << (i != j ? 1 : -1) << " " ; cout << endl; }
	}
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
#endif
    int t = in; while(t--) solve();
}
